package com.secneo.appshare.common;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class MD5 {
	 
	/**
	 * 加密
	 * 
	 * @param inputs
	 * @param type
	 *            默认 SHA-256其他SHA-1,MD5
	 * @return
	 */
	public static String encrypt(byte[] inputs, String type) {
	if (inputs == null) {
	return "";
	}
	MessageDigest md = null;
	String strDes = "";
	try {
	if (type == null || type.equals("")) {
	type = "SHA-256";
	}
	md = MessageDigest.getInstance(type);
	md.update(inputs);
	strDes = bytes2Hex(md.digest());
	} catch (NoSuchAlgorithmException e) {
		e.printStackTrace();
	}
	return strDes;
	}
	private static String bytes2Hex(byte[] bts) {
		String des = "";
		String tmp = null;
		for (int i = 0; i < bts.length; i++) {
		tmp = (Integer.toHexString(bts[i] & 0xFF));
		if (tmp.length() == 1) {
		des += "0";
		}
		des += tmp;
		}
		return des;
		}
		 

	
	public static String getmd5ByAPK(String pathApk,String md5Type)
	{
	  int n = 512; 
	  String md5String="";
	  byte buffer[] = new byte[n];   
	  try {   
	    // 创建文件输入流对象   
	  //  FileInputStream is = new FileInputStream("TestFile.txt");
		  if(!"".equals(pathApk)&&pathApk!=null)
		  {
			  FileInputStream is = new FileInputStream(pathApk);
			  // 读取输入流   
			    while ((is.read(buffer, 0, n) != -1) && (n > 0)) {   
			      
			    }   
			   // System.out.println();   
			    // 关闭输入流   
			    is.close();   
			    md5String=encrypt(buffer,md5Type);
			    }
	  }catch (IOException ioe) {   
			    System.out.println(ioe);   
			    } catch (Exception e) {   
			    System.out.println(e);   
			    }   
			  //  Log.v("md5....",getMD5(buffer));
	  return md5String;
	    // 设定读取的字节数   
	 
			  
	   

	}
	}